﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using Microsoft.Phone.Info;
using MTGScore_Cards.Resources;
using System.IO.IsolatedStorage;

namespace MTGScore_Cards
{
    public class CardLanguage
    {
        public string name;
        public string url;
        public CardLanguage()
        {
            name = "";
            url = "";
        }
    }
    public class CardEdiction
    {
        public string name;
        public string url;
        public CardEdiction()
        {
            name = "";
            url = "";
        }
    }
    public class Card
    {
        public string name;
        public string ccc;
        public string cc;
        public string image;
        public string type;
        public string text;
        public List<string> faqs;
        public List<string> legals;
        public List<CardEdiction> edictions;
        public List<CardLanguage> languages;
        public int succ;
        public Card()
        {
            name = "";
            ccc = "";
            cc = "";
            image = "";
            type = "";
            text = "";
            faqs = new List<string>();
            legals = new List<string>();
            languages = new List<CardLanguage>();
            edictions = new List<CardEdiction>();
            succ = 0;
        }
    }
    public class SearchResult
    {
        public List<Card> cards;
        public SearchResult()
        {
            cards = new List<Card>();
        }
    }
    public static class Research
    {
        public static void getHTML(string query, string options, Action<string> callback, Action<string, string> callback2, Action error)
        {
            try
            {
                HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(query);
                req.Method = "GET";
                req.BeginGetResponse(
                    (asyncResult) =>
                    {
                        try
                        {
                            HttpWebResponse objResponse = null;
                            StreamReader objs = null;

                            HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;

                            objResponse = (HttpWebResponse)request.EndGetResponse(asyncResult);
                            objs = new StreamReader(objResponse.GetResponseStream());

                            var s = objs.ReadToEnd();
                            if (callback2 != null)
                                callback2(s, options);
                            else if (callback != null)
                                callback(s);

                            return;
                        }
                        catch (Exception)
                        {
                            error();
                        }
                    }
                    , req);
            }
            catch (Exception)
            {
                error();
            }
        }
        public static void getHTML(string query, Action<string> callback, Action error)
        {
            getHTML(query, "", callback, null, error);
        }


        public static void getHTML(string query, string options, Action<string, string> callback, Action error)
        {
            getHTML(query, options, null, callback, error);
        }

        public static string GetUtilizationUrl(bool isTrial)
        {
            return GetUtilizationUrl(0, isTrial);
        }

        public static string GetUtilizationUrl(int occ, bool isTrial)
        {

            var s = "http://fboscari.altervista.org/submit_Utilizzi.php?name=" +
                Strings.Title +
                (isTrial ? " Trial" : "") +
                "&id=" +
                getID();
            if (occ >= 1)
                s += "&occ=" +
                    occ;
            return s;

        }
        public static string getID()
        {
            byte[] result = null;
            object uniqueId;
            if (DeviceExtendedProperties.TryGetValue("DeviceUniqueId", out uniqueId))
                result = (byte[])uniqueId;
            var s = "";
            for (int i = 0; i < result.Length; i++)
                s += result[i];
            return s;
        }

        #region utilizations
        private static Func<StreamReader, int> read_utilizations = reader =>
        {

            var s = reader.ReadToEnd();
            var occ = Int16.Parse(s);


            return occ;
        };


        private static Action<StreamWriter, int> write_utilizations = (writer, occ) =>
        {
            var s = "" + occ;

            writer.Write(s);
        };



        public static void SaveUtilizations(int utilizations)
        {
            using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                appStorage.DeleteFile("utilizations.txt");
                using (var streamFile = appStorage.OpenFile("utilizations.txt", FileMode.OpenOrCreate))
                using (var writer = new StreamWriter(streamFile))
                    write_utilizations(writer, utilizations);
            }
        }



        public static void DeleteUtilizations()
        {
            var appStorage = IsolatedStorageFile.GetUserStoreForApplication();
            appStorage.DeleteFile("utilizations.txt");
        }

        public static int LoadUtilizations()
        {
            using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var streamFile = appStorage.OpenFile("utilizations.txt", FileMode.OpenOrCreate))
                {
                    try
                    {
                        using (var reader = new StreamReader(streamFile))
                            return read_utilizations(reader);
                    }
                    catch (Exception)
                    {
                        appStorage.DeleteFile("utilizations.txt");
                        return 0;
                    }
                }


            }

        }
        #endregion
    }

}
